package com.cskaoyan.market.service.Impl;

import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.service.MarketConfigService;
import com.cskaoyan.market.vo.MarketConfigExpressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 付诚
 * @Date: 2024/5/20 09:18
 * @Description:
 */
@Service
public class MarketConfigServiceImpl implements MarketConfigService {
    @Autowired
    private MarketSystemMapper marketSystemMapper;

    @Override
    public Map<String, String> mall() {
        Map<String, String> data = new HashMap<>();
        MarketSystemExample marketSystemExample = new MarketSystemExample();

        List<MarketSystem> marketSystemList = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        marketSystemList.forEach(marketSystem -> {
            data.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        });

        return data;
    }

    @Override
    public void mallUpdate(Map<String, String> marketSystemMap) {

        marketSystemMap.forEach((key, value) -> {
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            MarketSystem marketSystem = new MarketSystem();
            criteria.andKeyNameEqualTo(key);
            marketSystem.setKeyValue(value);
            marketSystem.setUpdateTime(LocalDateTime.now());
            // todo 此时全部都更新时间，有误
            marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        });
    }

    @Override
    public Map<String, String> express() {

        Map<String, String> data = new HashMap<>();

        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameBetween("market_express_freight_min", "market_express_freight_value");

        List<MarketSystem> marketSystemList = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        marketSystemList.forEach(marketSystem -> {
            data.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        });

        return data;
    }

    @Override
    public void mallUpdate(MarketConfigExpressVo marketExpressVo) {
        // 第一次更新
        MarketSystem marketSystem = new MarketSystem();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameEqualTo("market_express_freight_min");
        marketSystem.setKeyValue(marketExpressVo.getMarket_express_freight_min());
        marketSystem.setUpdateTime(LocalDateTime.now());
        marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);

        // 第二次更新
        MarketSystem marketSystem2 = new MarketSystem();
        MarketSystemExample marketSystemExample2 = new MarketSystemExample();
        marketSystemExample2.createCriteria().andKeyNameEqualTo("market_express_freight_value");
        marketSystem2.setKeyValue(marketExpressVo.getMarket_express_freight_value());
        marketSystem2.setUpdateTime(LocalDateTime.now());
        marketSystemMapper.updateByExampleSelective(marketSystem2, marketSystemExample2);
    }

    @Override
    public void orderUpdate(Map<String, String> marketOrderMap) {

        MarketSystem marketSystem = new MarketSystem();

        marketOrderMap.forEach((key, value) -> {
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

            criteria.andKeyNameEqualTo(key);
            marketSystem.setKeyValue(value);
            marketSystem.setUpdateTime(LocalDateTime.now());
            marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        });
    }

    @Override
    public Map<String, String> order() {
        Map<String, String> data = new HashMap<>();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        List<String> list = new ArrayList<>();
        list.add("market_order_unconfirm");
        list.add("market_order_unpaid");
        list.add("market_order_comment");

        criteria.andKeyNameIn(list);

        List<MarketSystem> marketSystemList = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        marketSystemList.forEach(marketSystem -> {
            data.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        });
        return data;
    }

    @Override
    public void wxUpdate(Map<String, String> marketWxMap) {

        MarketSystem marketSystem = new MarketSystem();
        marketWxMap.forEach((key, value) -> {
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            criteria.andKeyNameEqualTo(key);
            marketSystem.setKeyValue(value);
            marketSystem.setUpdateTime(LocalDateTime.now());
            marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        });
    }

    @Override
    public Map<String, String> wx() {
        Map<String, String> data = new HashMap<>();

        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_wx_index_new");
        list.add("market_wx_index_topic");
        list.add("market_wx_index_brand");
        list.add("market_wx_index_share");
        list.add("market_wx_index_hot");
        list.add("market_wx_catlog_goods");
        list.add("market_wx_catlog_list");

        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystemList = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        marketSystemList.forEach(marketSystem -> {
            data.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        });
        return data;
    }


}
